﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace DreamBox.Net.Web
{
    public class ViewDataDictionary:IDictionary<string,object>
    {
        private readonly Dictionary<string, object> _innerDictionary = new Dictionary<string, object>();

        public ViewDataDictionary()
        { 
        }

        public ViewDataDictionary(ViewDataDictionary dictionary)
        {
            if (dictionary == null){
                throw new ArgumentNullException("dictionary");
            }
            foreach (var entry in dictionary){
                _innerDictionary.Add(entry.Key, entry.Value);
            }
        }

        public int Count
        {
            get {
                return _innerDictionary.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return ((IDictionary<string, object>)_innerDictionary).IsReadOnly;
            }
        }

        public ICollection<string> Keys
        {
            get
            {
                return _innerDictionary.Keys;
            }
        }

        public object this[string key]
        {
            get
            {
                object value;
                if (!_innerDictionary.TryGetValue(key, out value))
                {
                    Add(key, "");
                    _innerDictionary.TryGetValue(key, out value);
                }
                return value;
            }
            set
            {
                _innerDictionary[key] = value;
            }
        }
        public ICollection<object> Values
        {
            get
            {
                return _innerDictionary.Values;
            }
        }

        public void Add(KeyValuePair<string, object> item)
        {
            ((IDictionary<string, object>)_innerDictionary).Add(item);
        }

        public void Add(string key, object value)
        {
            _innerDictionary.Add(key, value);
        }

        public void Clear()
        {
            _innerDictionary.Clear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            return ((IDictionary<string, object>)_innerDictionary).Contains(item);
        }

        public bool ContainsKey(string key)
        {
            return _innerDictionary.ContainsKey(key);
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            ((IDictionary<string, object>)_innerDictionary).CopyTo(array, arrayIndex);
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            return ((IDictionary<string, object>)_innerDictionary).Remove(item);
        }

        public bool Remove(string key)
        {
            return _innerDictionary.Remove(key);
        }

        public bool TryGetValue(string key, out object value)
        {
            return _innerDictionary.TryGetValue(key, out value);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_innerDictionary).GetEnumerator();
        }
    }
}
